package lianxi;

import java.util.Stack;

public class demo5 {
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
    public class Solution {
        public ListNode ReverseList(ListNode head) {
            Stack<ListNode> stack=new Stack<>();
           while (head!=null){
               stack.push(head);
               head=head.next;
           }
            ListNode node=stack.pop();
           ListNode dummy=node;
           while (!stack.isEmpty()){
           ListNode listNode=stack.pop();
           node.next=listNode;
               node=node.next;
           }
           node.next=null;
           return dummy;
        }
        public ListNode ReverseList1(ListNode head) {
            //终止
            if(head==null||head.next==null){
                return head;
            }
            //保存当前节点的下一个节点
               ListNode next=head.next;
            //从当前节点的下一个节点递归调用
            ListNode rever=ReverseList1(next);
            next.next=head;

            head.next=null;
            return rever;
        }
    }
}
